'use strict'

const get_courses = require('./getCourse')
const config = require('./config')
const fs = require('fs')
const save = require('./save_toJSon')
const del = require('del')
const Courses = require('./save_toJSon')

let course_urls = process.argv.slice(2);
if (course_urls.length > 0) {
    console.log('course  url is:' + course_urls.toString());
    fs.exists(config.saveDir, isExist => {
        if (!isExist) {
            fs.mkdir(saveDir, err => {
                if (err) {
                    return console.err(err);
                }
                return console.log('目录创建成功！');
            })
        }
    })
    if (course_urls.length > 0) get_courses(course_urls)
} else {
    // 如果没有给定课程地址，将下载 config中lession_urls获取所有的课程连接并存至data/data.json中
    del(['../data/**'], { force: true })
        .then(paths => {
            fs.mkdir('../data', err => {
                if (err) {
                    return console.error(new Error('create data file fail!!'));
                }
            })
        })
    config.lession_urls.map(lession => {
        let s = new Courses(lession)
        s.cousre().then(res => {
            res.map(c => s.video(c))
        })
    })
}